<?php
namespace app\common\library;

//import('API-PHP.HttpClient',VENDOR_PATH,'.class.php');


use app\common\library\HttpClient;

class Feie
{
    public function __construct(){
        define('USER', '1581206083@qq.com');  //*必填*：飞鹅云后台注册账号
        define('UKEY', 'QtFjJkXKtTkf6kWG');  //*必填*: 飞鹅云后台注册账号后生成的UKEY 【备注：这不是填打印机的KEY】
        define('SN', '960812163');      //*必填*：打印机编号，必须要在管理后台里添加打印机或调用API接口添加之后，才能调用API
        //以下参数不需要修改
        define('IP','api.feieyun.cn');      //接口IP或域名
        define('PORT',80);            //接口IP端口
        define('PATH','/Api/Open/');    //接口路径
    }


    //处理订单信息
    public function printOrderInfo($tid,$es_id){
        $order_model = new \app\admin\model\select\Order();
        $enterprise_model = new \app\admin\model\Enterprise();
        $enterprise       = $enterprise_model->find($es_id);
        $trade       = $order_model->find($tid);
        $str         = $this->_beforehand_order_str($trade,$enterprise);
        $this->printMsg(SN,$str,1);
    }


    private function _beforehand_order_str($trade,$enterprise){
        // 获取收货地址

        if($enterprise){
            $str =  '';
            $str .=  '<CB>'."{$enterprise['mde_name']}".'</CB><BR>';
            $str .=  '<BR>';

            $field = json_decode($trade['o_field'],true);
            if($trade['o_type'] == 1){
                $str .= '客户:'.$trade['o_in_uname']. '<BR>';
                $str .= '联系电话:'.$trade['o_in_phone']. '<BR>';
                $str .= '收货地址:'.$field['收货地址']. '<BR>';
                $str .= '出库单是否随货发走:'.$field['出库单是否随货发走']. '<BR>';
                $str .= '是否已收款:'.$field['是否已收款']. '<BR>';
            }else{
                $str .= '供货商:'.$trade['o_out_uname']. '<BR>';
                $str .= '联系电话:'.$trade['o_out_phone']. '<BR>';
            }
            $str .= '备注:'.$trade['o_note']. '<BR>';

            $order_goods_model = new \app\common\model\OrderGoods();
            $where           = ['og_order_id'=>$trade['o_id']];
            $storage_info      = $order_goods_model->where($where)->select();
            $cate_model = new \app\admin\model\select\Category();

            foreach ($storage_info as $vv){
                $cate  = $cate_model->find($vv['og_cate_id']);
                $str .=  ($vv['og_year']?$vv['og_year'].'年'.$cate['c_title']:$cate['c_title']).(isset($vv['og_specs'])?'('.$vv['og_specs'].')':'').' : '.$vv['og_num'].'(￥'.$vv['og_price'].')'.'<BR>';
            }

        }

        return $str;
    }


    /**
     * [打印订单接口 Open_printMsg]
     * @param  [string] $sn      [打印机编号sn]
     * @param  [string] $content [打印内容]
     * @param  [string] $times   [打印联数]
     * @return [string]          [接口返回值]
     */
    public function printMsg($sn,$content,$times){
        $time = time();         //请求时间
        $msgInfo = array(
            'user'=>USER,
            'stime'=>$time,
            'sig'=>$this->signature($time),
            'apiname'=>'Open_printMsg',
            'sn'=>$sn,
            'content'=>$content,
            'times'=>$times//打印次数
        );
        $client = new HttpClient(IP,PORT);
        if(!$client->post(PATH,$msgInfo)){
            return false;
        }else{
            //服务器返回的JSON字符串，建议要当做日志记录起来
            $result = $client->getContent();
            echo true;
        }
    }


      /**
     * [signature 生成签名]
     * @param  [string] $time [当前UNIX时间戳，10位，精确到秒]
     * @return [string]       [接口返回值]
     */
    private function signature($time){
        return sha1(USER.UKEY.$time);//公共参数，请求公钥
    }


}